/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.gui.mvc.controller.testbed;

import org.testis.gui.mvc.controller.AbstractController;
import org.testis.gui.mvc.controller.TestBedControllerList;
import org.testis.gui.mvc.model.testbed.DeviceDefinitionModel;
import org.testis.gui.mvc.view.testbed.DeviceDefinitionView;
import org.testis.orm.refinedclasses.DeviceDefinitionRefined;
import org.testis.orm.refinedclasses.TestBedItem;
import org.testis.utils.middleware.DeviceDefinitionDataHandler;
import org.testis.utils.tree.TbedMutableTreeNode;

/**
 *
 * @author selcuk
 */
public class DeviceDefinitionController extends AbstractController {

    public static final String VISIBLE_PROPERTY = "Visibility";
    public static final String ERROR_PROPERTY = "LastError";
    public static final String DEVICE_ID_PROPERTY = "DeviceId";
    public static final String NAME_PROPERTY = "Name";
    public static final String ABSTRACT_GROUP_PROPERTY = "AbstractGroupId";
    private TbedMutableTreeNode m_selectedNode = null;

    public DeviceDefinitionController() {
        super();
    }

    private void initialize(boolean editMode) {
        DeviceDefinitionModel model = new DeviceDefinitionModel();
        DeviceDefinitionView view = new DeviceDefinitionView(
                TestBedControllerList.newInstance().getParentFrame(),
                true, editMode);
        super.addModel(model);
        super.addView(view);
        view.pack();
    }

    public void operationCloseWindow() {
        super.cleanAll();
    }

    public void operationNewDeviceDefinition() {
        initialize(false);
        setModelProperty(VISIBLE_PROPERTY, true);
    }

    public void operationEditDevice(TbedMutableTreeNode node) {
        if (node.getUserObject() instanceof DeviceDefinitionRefined) {
            m_selectedNode = node;
            DeviceDefinitionRefined device = getSelectedDevice();
            if (device == null) {
                System.out.println(
                        "Not found design part for DeviceDefinitionRefined");
                return;
            }
            initialize(true);
            setModelProperty(DEVICE_ID_PROPERTY, device.getId());
            setModelProperty(NAME_PROPERTY, device.getName());
            setModelProperty(ABSTRACT_GROUP_PROPERTY,
                    device.getAbstractGroup().getId());
            setModelProperty(VISIBLE_PROPERTY, true);
        }
    }

    public void operationEditSelectedDevice() {
        TbedMutableTreeNode node = TestBedControllerList.newInstance().getTbedTreeController().getSelectedTreeNode();
        operationEditDevice(node);
    }

    public void operationAddDevice(String name, int abstractId) {
        DeviceDefinitionRefined refined = DeviceDefinitionFactory.newInstance().createDevice(
                name, abstractId);
        if (refined != null) {
            TestBedControllerList.newInstance().getTbedTreeController().operationAddTestBedItem(
                    refined);
        } else {
            setModelProperty(ERROR_PROPERTY, "Unable to add device record");
        }
    }

    public void operationSaveDevice(int id, String name, int abstractId) {
        boolean result = DeviceDefinitionDataHandler.newInstance().saveDeviceDefinition(
                id, name,
                abstractId);
        if (!result) {
            setModelProperty(ERROR_PROPERTY, "Unable to edit device record");
        }
    }

    public void operationDeleteDevice(int id) {
        if (DeviceDefinitionFactory.newInstance().deleteDevice(id)) {
            TestBedControllerList.newInstance().getTbedTreeController().operationRemoveSelectedTestBedItem();
        } else {
            TestBedControllerList.newInstance().getTbedTreeController().operationSetErrorProperty(
                    "Unable to remove device record");
        }
    }

    public void operationDeleteSelectedDevice() {
        TestBedItem tbedItem = TestBedControllerList.newInstance().getTbedTreeController().getSelectedTestBedItem();
        if (tbedItem != null && tbedItem instanceof DeviceDefinitionRefined) {
            DeviceDefinitionRefined refined = (DeviceDefinitionRefined) tbedItem;
            operationDeleteDevice(refined.getId());
        }
    }


    public DeviceDefinitionRefined getSelectedDevice() {
        DeviceDefinitionRefined result = null;
        Object obj = m_selectedNode.getUserObject();
        if (obj != null && obj instanceof DeviceDefinitionRefined) {
            result = (DeviceDefinitionRefined) obj;
        }
        return result;
    }
}
